package mysql

import (
	"gorm.io/gorm"
	"wechatdev/wechatdevrpc/wechatdevrpc"
)

type User struct {
	gorm.Model
	HeadImage string `gorm:"type:varchar(255)"`
	NickName  string `gorm:"type:varchar(30)"`
	Address   string `gorm:"type:varchar(30)"`
	Mobile    string `gorm:"type:varchar(30)"`
	Password  string `gorm:"type:varchar(32)"`
	WxCode    string `gorm:"type:varchar(30)"`
	Qq        string `gorm:"type:varchar(30)"`
	Email     string `gorm:"type:varchar(30)"`
}

func (u *User) FindUser(db *gorm.DB, mobile string) error {
	return db.Debug().Where("mobile=?", mobile).Find(u).Error
}

func (u *User) RedisterUser(db *gorm.DB) error {
	return db.Debug().Create(u).Error
}

func (u *User) FindByUser(db *gorm.DB, mobile string) error {
	return db.Debug().Where("mobile=?", mobile).Find(u).Error
}

func (u *User) FindUserAccount(db *gorm.DB, account string) error {
	return db.Debug().Where("wx_code=? or qq=? or email=?", account, account, account).Find(u).Error
}

func (u *User) UserPerfacet(db *gorm.DB) error {
	return db.Debug().Updates(u).Error
}
func UserShow(db *gorm.DB, id int64) ([]*wechatdevrpc.UserShow, error) {
	var show []*wechatdevrpc.UserShow
	db.Debug().Model(&User{}).Select(
		"id as user_id",
		"head_image",
		"address",
		"nick_name",
		"mobile",
		"password",
		"qq",
		"wx_code",
		"email",
	).Where("users.id=?", id).First(&show)
	return show, err
}
